Imports System.Xml
Imports System.Net
Imports System.IO
Imports System.Deployment
Imports System.Xml.Linq




' *******************************************************
' *   NABAZTAG CONTROL CENTER                           *
' *   (C) Philip Holtom 2006 / 2007                     *
' *    P.Holtom@gmail.com or philip@philipholtom.co.uk  *
' *                                                     *
' *    You may modify this code freely                  *
' *    AS LONG AS YOU SEND ME A COPY                    *
' *    AND CREDIT ME FOR THE ORIGINAL CODE              *
' *                                                     *
' * I am just learning VB. Bear with me. I know the     *
' * code is messy in places and lacks elegance          *
' *******************************************************



Public Class Form1


    ' *****************************************************
    ' *   DEFINE BITS AND PIECES                          *
    ' *                                                   *
    ' *****************************************************
    Public WithEvents TrayIcon As NotifyIcon
    Private Shownn As Boolean
    Public netresult = False
    ' Define the Class to commuincate with the API
    Dim Api As New Nabaztag_API


    ' *****************************************************
    ' *   SUBS TO SEND COMMANDS TO THE API                *
    ' *                                                   *
    ' *****************************************************




    Private Sub mnlsend()

        Try
            Dim api_return As String

            ' If the manual textbox is blank then send the selected stored message

            ErrorProvider1.Clear()

            If token.Text = "" Then
                ErrorProvider1.SetError(token, "Please Input A Value")

            End If
            If serial.Text = "" Then
                ErrorProvider1.SetError(serial, "Please Input a value")
            End If
            If mntts.Text = "" Then
                ' Clear the boxes
                urioutput.Clear()
                TextBox2.Clear()

                ' Send the command to the API
                api_return = Api.sendapi(storedmessages.Text, serial.Text, token.Text, voicebox.Text)

                ' Split the string that we combined in the class as VB only returns one value in a function to obtain the XML output of the API

                Dim stringItems() As String = api_return.Split("[")
                ' Clear the boxes
                urioutput.Clear()
                TextBox2.Clear()

                ' Display the URI we sent to the api. This is useful for debugging to see what goes on
                urioutput.Text = stringItems(0)
                ' Display the message the API returns.
                TextBox2.Text = stringItems(1)

            Else

                ' If the manual send box is not blank do this
                api_return = Api.sendapi(mntts.Text, serial.Text, token.Text, voicebox.Text)

                Dim stringItems() As String = api_return.Split("[")
                ' Display the URI we sent to the api. This is useful for debugging to see what goes on
                urioutput.Text = stringItems(0)
                ' Display the message the API returns.
                TextBox2.Text = stringItems(1)










            End If
        Catch exp As Exception

            MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try


    End Sub


    ' *****************************************************
    ' *  MONITOR THE NETWORK TO SEE IF THE WEBSITE IS UP  *
    ' *                                                   *
    ' *****************************************************

    Public Sub TestNetwork()



        ' Checks the value of the text.
        Dim isAvailable As Boolean
        isAvailable = My.Computer.Network.IsAvailable
        If isAvailable = False Then
            ' Initializes variables to pass to the MessageBox.Show method.

            Dim Message As String = "Warning: Did not detect a network connection. You need a working internet connection to use this program. Do you want to quit ?"
            Dim Caption As String = "Error Detected in Network Connection"
            Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo

            Dim Result As DialogResult

            'Displays the MessageBox

            Result = MessageBox.Show(Message, Caption, Buttons)

            ' Gets the result of the MessageBox display.

            If Result = System.Windows.Forms.DialogResult.Yes Then

                ' Closes the parent form.
                netresult = True
                Me.Close()


            End If

        End If

    End Sub


    ' *****************************************************
    ' *   Button Handling                                 *
    ' *                                                   *
    ' *****************************************************


    ' On clicking the "Message my Rabbit Button call the sub to connect to the api

    Private Sub Send_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles send.Click
        mnlsend()
        Getmessages()

    End Sub




    ' *****************************************************
    ' *   MENU ITEMS                                      *
    ' *                                                   *
    ' ***************************************************** 


    ' Show the My Warren Menu
    Private Sub MyWarrenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyWarrenToolStripMenuItem.Click
        Dim Warren As New Warren
        Warren.Show()

    End Sub


    ' Show the My Messages Menu Item

    Private Sub MyMessagesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyMessagesToolStripMenuItem.Click
        Dim messages As New Messages
        messages.Show()

    End Sub

    ' Show the Ears Menu Item

    Private Sub MyEarsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyEarsToolStripMenuItem.Click
        Dim ears As New Ear_Position
        ears.Show()

    End Sub

    ' Show the About Menu

    Private Sub AboutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem1.Click
        Dim About As New AboutBox
        About.Show()
    End Sub

    ' Show the Instructions Menu

    Private Sub InstructionsToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstructionsToolStripMenuItem1.Click
        Dim instruct As New Instructions
        instruct.Show()
    End Sub

    ' Show the Radio Menu

    Private Sub RSSReaderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RSSReaderToolStripMenuItem.Click
        Dim radio As New Radio_List
        radio.Show()

    End Sub

    ' Show the admin Menu
    Private Sub AdminSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdminSettingsToolStripMenuItem.Click
        Dim admin As New Admin
        admin.Show()

    End Sub


    ' *****************************************************
    ' *   FORM ITEMS                                      *
    ' *                                                   *
    ' *****************************************************


   

    ' Code to run on the form loading

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        token.Text = "1167205173"
        serial.Text = "0013D3824BED"
        PictureBox1.Hide()
        PictureBox2.Hide()


        Dim fileExists As Boolean
        fileExists = My.Computer.FileSystem.FileExists("Rabbit.mdb")



        If fileExists = True Then

        Else
            MsgBox("Warning ! Rabbit.mdb database file NOT found. Please reinstall or contact nabaztag@philipholtom.co.uk for assitance", MsgBoxStyle.Critical, "WARNING")
            Me.Close()

        End If

        'TODO: This line of code loads data into the 'Radio._Radio' table. You can move, or remove it, as needed.
        Me.RadioTableAdapter.Fill(Me.Radio._Radio)
        'TODO: This line of code loads data into the 'Rabbitsettings.Settings' table. You can move, or remove it, as needed.
        Me.SettingsTableAdapter.Fill(Me.Rabbitsettings.Settings)
        'TODO: This line of code loads data into the 'Rabbitsettings.Settings' table. You can move, or remove it, as needed.
        Me.SettingsTableAdapter.Fill(Me.Rabbitsettings.Settings)



        'TODO: This line of code loads data into the 'Messagedata.Messages' table. You can move, or remove it, as needed.
        Me.MessagesTableAdapter.Fill(Me.Messagedata.Messages)
        'TODO: This line of code loads data into the 'RabbitDataSet.Rabbit' table. You can move, or remove it, as needed.
        Me.RabbitTableAdapter.Fill(Me.RabbitDataSet.Rabbit)

        ' !!! You have to add this code to the Form.Load Event Handler !!!

        TrayIcon = nficon ' Makes a new instance of the tray icon (this
        '                           automatically creates it on the System Tray

        Shownn = True '              The form is currently shown, so...
        'TrayIcon.Visible = False


        With TrayIcon '                         Let's fill the tray icon
            .Visible = False '                  Becouse the form is currently
            '                                   shown, let's hide the tray icon
            .Icon = Me.Icon '                   You can define the image to be
            '                                   shown here, i put the icon of the
            '                                   form
            .Text = "Bring Your Rabbit to Life !" '   This is the tool tip text that 
            '                                   shows the icon (You can change it
            '                                   dinamicaly)
            '.ContextMenu = Me.IconMenu '        This tells the icon to show the 
            '                                   popup menu defined in the form
        End With

        ' Test the network connection
        ' If we can't reach the Nabaztag site then warn the user.

        TestNetwork()


    End Sub


    ' This handles placing an icon in the system tray when the form is minimized

    Private Sub MenuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrayIcon.Click
        ' This handler restores the window when you click open in the 
        ' nMenu-Open or when you double-click on the tray icon

        Me.WindowState = FormWindowState.Normal '   Restores the window
        Me.ShowInTaskbar = True '                   Show the TaskBar button
        Shownn = True '                              The form is currently shown, so...
        TrayIcon.Visible = False '                  Hides the tray icon

    End Sub


    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        ' This handler hides the Taskbar button of the window and shows the tray icon
        ' if the form was minimized
        If Shownn And Me.WindowState = FormWindowState.Minimized Then
            Shownn = False '                 The form is currently hided, so...
            Me.ShowInTaskbar = False '      Hides the Taskbar button of the form
            TrayIcon.Visible = True '       Shows the tray icon
        End If
    End Sub









    ' *****************************************************
    ' *   UPDATE THE MESSAGE TABLE                        *
    ' *                                                   *
    ' *****************************************************


    Private Sub ComboBox2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox2.MouseClick
        Me.RabbitTableAdapter.Fill(Me.RabbitDataSet.Rabbit)
    End Sub

    Private Sub ComboBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox1.MouseClick
        Me.RadioTableAdapter.Fill(Me.Radio._Radio)

    End Sub

    Private Sub ComboBox3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles storedmessages.MouseClick
        Me.MessagesTableAdapter.Fill(Me.Messagedata.Messages)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.MessagesTableAdapter.Insert(mntts.Text)
        Me.MessagesTableAdapter.Update(Me.Messagedata.Messages)
        Me.Messagedata.AcceptChanges()
    End Sub





    ' *****************************************************
    ' *   UPDATE THE PROGRAM                              *
    ' *                                                   *
    ' *****************************************************


    Private Sub CheckForUpdatesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckForUpdatesToolStripMenuItem.Click
        Try

            If My.Application.IsNetworkDeployed = True Then

                If My.Application.IsNetworkDeployed() Then
                    If My.Application.Deployment.CheckForDetailedUpdate.UpdateAvailable = True Then
                        My.Application.Deployment.Update()
                        MessageBox.Show("Updated to Version " & My.Application.Deployment.UpdatedVersion.ToString)

                    End If

                    If My.Application.Deployment.CheckForDetailedUpdate.UpdateAvailable = False Then
                        MessageBox.Show("No updates found!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    End If

                End If
            Else
                MsgBox("Please check http://www.philipholtom.co.uk/software for updates")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End Try


    End Sub



















    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try



            Dim stUri As String
            stUri = "http://api.nabaztag.com/vl/FR/api_stream.jsp?sn=" & serial.Text & "&token=" & token.Text & "&urlList=" & radiourlbox.Text

            urioutput.Text = stUri



            Using reader As XmlReader = XmlReader.Create(stUri)

                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.IsEmptyElement Then
                            MsgBox("<{0}/>", reader.Name)

                        Else
                            reader.Read() ' Read the start tag.
                            If reader.IsStartElement() Then ' Handle nested elements.

                                Dim output As String

                                output = reader.ReadString()
                                TextBox2.Text = output
                                reader.ReadEndElement()
                                output = reader.ReadString()
                                TextBox2.Text = output
                            End If

                        End If
                    End If
                End While

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try

    End Sub




    Private Sub SkypeSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Skype As New Skype
        Skype.Show()

    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub GetVersionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetVersionToolStripMenuItem.Click
        Try



            Dim stUri As String
            stUri = Api.actionapi(serial.Text, token.Text, 8)

            urioutput.Text = stUri



            Using reader As XmlReader = XmlReader.Create(stUri)

                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.IsEmptyElement Then
                            MsgBox("<{0}/>", reader.Name)

                        Else
                            reader.Read() ' Read the start tag.
                            If reader.IsStartElement() Then ' Handle nested elements.

                                Dim output As String

                                output = reader.ReadString()
                                MsgBox(output)
                                'reader.ReadEndElement()
                                'output = reader.ReadString()
                                'TextBox2.Text = output
                            End If

                        End If
                    End If
                End While

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try

    End Sub


    Private Sub SendYourRabbitToSleepToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendYourRabbitToSleepToolStripMenuItem.Click
        Try



            Dim stUri As String
            stUri = Api.actionapi(serial.Text, token.Text, 13)

            urioutput.Text = stUri



            Using reader As XmlReader = XmlReader.Create(stUri)

                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.IsEmptyElement Then
                            MsgBox("<{0}/>", reader.Name)

                        Else
                            reader.Read() ' Read the start tag.
                            If reader.IsStartElement() Then ' Handle nested elements.

                                Dim output As String

                                output = reader.ReadString()

                                reader.ReadEndElement()
                                output = reader.ReadString()
                                MsgBox(output)
                            End If

                        End If
                    End If
                End While

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try
    End Sub

    Private Sub WakeUpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WakeUpToolStripMenuItem.Click
        Try



            Dim stUri As String
            stUri = Api.actionapi(serial.Text, token.Text, 14)

            urioutput.Text = stUri



            Using reader As XmlReader = XmlReader.Create(stUri)

                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.IsEmptyElement Then
                            MsgBox("<{0}/>", reader.Name)

                        Else
                            reader.Read() ' Read the start tag.
                            If reader.IsStartElement() Then ' Handle nested elements.

                                Dim output As String

                                output = reader.ReadString()

                                reader.ReadEndElement()
                                output = reader.ReadString()
                                MsgBox(output)
                            End If

                        End If
                    End If
                End While

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try
    End Sub

    Private Sub IsTheNabaztagAwakeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IsTheNabaztagAwakeToolStripMenuItem.Click
        Try



            Dim stUri As String
            stUri = Api.actionapi(serial.Text, token.Text, 7)

            urioutput.Text = stUri



            Using reader As XmlReader = XmlReader.Create(stUri)

                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.IsEmptyElement Then
                            MsgBox("<{0}/>", reader.Name)

                        Else
                            reader.Read() ' Read the start tag.
                            If reader.IsStartElement() Then ' Handle nested elements.

                                Dim output As String

                                output = reader.ReadString()
                                MsgBox(output)
                                'reader.ReadEndElement()
                                'output = reader.ReadString()
                                'MsgBox(output)
                            End If

                        End If
                    End If
                End While

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try
    End Sub



    Public Sub Getstatus()
        Try



            Dim stUri As String
            stUri = Api.actionapi(serial.Text, token.Text, 7)

            urioutput.Text = stUri



            Using reader As XmlReader = XmlReader.Create(stUri)

                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.IsEmptyElement Then
                            MsgBox("<{0}/>", reader.Name)

                        Else
                            reader.Read() ' Read the start tag.
                            If reader.IsStartElement() Then ' Handle nested elements.

                                Dim output As String

                                output = reader.ReadString()

                                If output = "NO" Then
                                    PictureBox1.Show()
                                    PictureBox2.Hide()

                                End If

                                If output = "YES" Then
                                    PictureBox1.Hide()
                                    PictureBox2.Show()

                                End If
                                'reader.ReadEndElement()
                                'output = reader.ReadString()
                                'MsgBox(output)
                            End If

                        End If
                    End If
                End While

            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try
    End Sub



    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Getstatus()

    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click

    End Sub

    Private Sub Getmessages()


        Dim myUrl As String = Api.actionapi("0013D3824BED", "1167205173", 3)





        Dim x As Integer


        Dim filename As String = myUrl
        Dim reader As New XmlTextReader(filename)

      


        Dim ds As New DataSet

        ds.ReadXml(reader)
        MsgBox(ds.Tables.Item(1).ToString)
        DataGridView1.DataSource = New BindingSource(ds, "msg")
        DataGridView1.Columns.Remove("url")
        DataGridView1.AutoResizeColumns()

        'While reader.Read
        'reader.MoveToContent()
        'For x = 0 To reader.AttributeCount - 1
        ' reader.MoveToAttribute(x)
        ' Next
        'End While

   

    End Sub

    

   
End Class
